package 剑指;

import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;

import java.util.Arrays;
import java.util.Collections;

public class offer45 {
    public String minNumber(int[] nums) {
        quickSort(nums,0,nums.length-1);

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < nums.length; i++) {
            sb.append(nums[i]);
        }
        return sb.toString();
    }

    private void quickSort(int[] nums,int start,int end) {
        if(start >= end) return;
        int pivot = partition(nums,start,end);

        quickSort(nums,start,pivot - 1);
        quickSort(nums,pivot + 1,end);
    }

    private int partition(int[] nums, int start, int end) {
        int tmp = nums[start];
        while(start < end){
            while(start < end && comp(nums,end,tmp) <= 0){
                end--;
            }
            nums[start] = nums[end];
            while(start < end && comp(nums,start,tmp) >=0){
                start++;
            }
            nums[end] = nums[start];
        }
        nums[start] = tmp;
        return start;
    }

    public int comp(int[] nums,int left,int tmp){
        String num1 = nums[left] +""+ tmp;
        String num2 = tmp +""+ nums[left] ;
        // >=0 => num1 > num2 => 交换
        // <0 => 不用交换
        System.out.println(num1);
        System.out.println(num2);
        return num1.compareTo(num2);
    }
}
